//
//  MJ_LanguageSelectController.m
//  UEAssistant
//
//  Created by mac on 2018/1/10.
//  Copyright © 2018年 youyixinxi. All rights reserved.
//

#import "MJ_LanguageSelectController.h"
#import "MJ_OperationViewModel.h"
#import "MJ_LanguageBaseModel.h"
#import "MJ_LanguageSearchBar.h"
@interface MJ_LanguageSelectController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic)UITableView *tableView;
@property (strong, nonatomic)MJ_OperationViewModel *viewModel;

@property (strong, nonatomic)MJ_LanguageSearchBar *searchBar;

@property (strong, nonatomic)UILabel *titleLabel;

@property (strong, nonatomic)UIButton *cancelBtn;

@property (strong, nonatomic)NSIndexPath *lastIndexPath;
@end

@implementation MJ_LanguageSelectController
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, Screen_Width, Screen_Height-70) style:UITableViewStylePlain];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorColor = [UIColor colorWithHexString:@"#e0e0e0"];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

- (MJ_OperationViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[MJ_OperationViewModel alloc] init];
    }
    return _viewModel;
}

- (MJ_LanguageSearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[MJ_LanguageSearchBar alloc] initWithFrame:CGRectMake(85, 24, Screen_Width-140, 35)];
        _searchBar.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
        _searchBar.placeholder = @"搜索";
        _searchBar.barTintColor = [UIColor whiteColor];
        _searchBar.layer.cornerRadius = 5.0;
        _searchBar.layer.masksToBounds = YES;
    }
    return _searchBar;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 24, 66, 35)];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _titleLabel.text = @"目标语言";
        _titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return _titleLabel;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:mainColor forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.frame = CGRectMake(Screen_Width-45, 24, 35, 35);
    }
    return _cancelBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.searchBar];
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.cancelBtn];
    [self.viewModel loadLanguagelistWithSuccess:^(BOOL success) {
        [self.tableView reloadData];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.viewModel.languagelist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    
    MJLanguageListModel *model = self.viewModel.languagelist[indexPath.row];
    cell.textLabel.text = model.dictionaryValue;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    cell.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    if (indexPath == _lastIndexPath) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:_lastIndexPath];
    lastCell.accessoryType = UITableViewCellAccessoryNone;
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    _lastIndexPath = indexPath;
   
    MJLanguageListModel *model = self.viewModel.languagelist[indexPath.row];
    self.SelectLanguageBlock(model.dictionaryKey, model.dictionaryValue);
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, tableView.sectionHeaderHeight)];
    label.backgroundColor = lineMainColor;
    label.text = @"    语言列表";
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor colorWithHexString:@"#666666"];
    return label;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 25;
}
- (void)cancel {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
